<?php
class attrtaglistService extends commonService{
	public function __construct(){
		parent::__construct();
		$this->table_index=table('index_1');
	}
    public function t(){
        return array('attrtaglist');
    }
    public function add_($arc_id,$str){
        $str=trim($str);
        $str=trim($str,',');
        if($str=='') return true;
        $tag_arr=$this->tag_explode($str);
        $tag_arr=$this->tag_unique($tag_arr);
        return $this->insert_tag($arc_id,$tag_arr);
    }
    //取得标签字符串
    public function mod($arc_id){
        $arr=$this->get_arc_tag_arr($arc_id);
        return $this->tag_implode($arr);
    }
    public function mod_($arc_id,$str){
        $str=trim($str);
        $str=trim($str,',');
		//现在标签可能为空
        //if($str=='') return true;
        $tag_arr=$this->tag_explode($str);
        $tag_arr=$this->tag_unique($tag_arr);
        
        $old_tag_arr=$this->get_arc_tag_arr($arc_id);
		//$old_tag_arr=array_map('addslashes',$old_tag_arr);
        
        list($del,$add)=$this->cal_tag($old_tag_arr,$tag_arr);
        
        $this->delete_tag($arc_id,$del);
        $this->insert_tag($arc_id,$add);
        return true;
    }
    //分割标签
    private function tag_explode($str){
        $arr=explode(',',$str);
        foreach($arr as $k=>$v){
            if(trim($v)=='') unset($arr[$k]);//去除空
        }
        return array_values($arr);
    }
    //组合标签
    private function tag_implode($arr){
        return implode(',',$arr);
    }
    //一篇文章标签唯一化
    private function tag_unique($data){
        return array_unique(array_map('trim',$data));
    }
    //检查标签是否存在
	private function tag_exist_0($tag){
		$query="select `id` from `{$this->table_index}` where `tag`=:tag limit 1";
		return $this->m->get_field($query,array('tag'=>$tag));
	}
    private function tag_exist($arc_id,$tag){
        $query="select `id` from `{$this->t[0]}` where `arc_id`=:arc_id and `tag`=:tag limit 1";
        return $this->m->get_field($query,array('arc_id'=>$arc_id,'tag'=>$tag));
    }
	//向index表插入数据
	private function treat_index_part($tag){
		if($tag=='') return false;
		$d=array();
		$d['tag']=$tag;
		$d['hits']=0;
		$d['w']=0;
		$d['m']=0;
		$d['w_up']=time();
		$d['m_up']=time();
		$d['seo_title']='';
		$d['seo_keywords']='';
		$d['seo_description']='';
		return $this->model->insert_by_array($this->table_index,$d);
	}
    //计算获得修改时的标签数组
    private function cal_tag($old,$new){
        //123
        //  345
        $del=array();
        $add=array();
        
        $intersect=array_intersect($old,$new);
        $del=array_diff($old,$intersect);
        $add=array_diff($new,$intersect);
        
        return array($del,$add);
    }
    
    //插入标签 $tag_arr 为数组
    private function insert_tag($arc_id,$tag_arr){
        if(!is_array($tag_arr)) return true;
        if(count($tag_arr)==0) return true;
        foreach($tag_arr as $k=>$v){
			$t=$this->tag_exist_0($v);
			if(!$t){
				$x=$this->treat_index_part($v);
				if(!$x) continue;
			}
		
            $r=$this->tag_exist($arc_id,$v);
            if(!$r){
                $this->model->insert_by_array($this->t[0],array('arc_id'=>$arc_id,'tag'=>$v));
            }
        }
        return true;
    }
    //删除标签
    private function delete_tag($arc_id,$tag_arr){
        if(!is_array($tag_arr)) return true;
        if(count($tag_arr)==0) return true;
        foreach($tag_arr as $k=>$v){
           $this->m->query("delete from `{$this->t[0]}` where `tag`=:tag and `arc_id`=:arc_id limit 1",array('tag'=>$v,'arc_id'=>$arc_id));
           
        }
        return true;
    }
	//删除文章时清理标签
	public function delete_all_tag($arc_id){
		$query="delete from `{$this->t[0]}` where `arc_id`=:arc_id";
		return $this->m->query($query,array('arc_id'=>$arc_id));
	}
    //取得文章标签数组
    private function get_arc_tag_arr($arc_id){
        $query="select `tag` from `{$this->t[0]}` where `arc_id`=:arc_id order by id asc";
        $info=$this->m->mselect($query,array('arc_id'=>$arc_id));
        return multi2single_min($info,'tag');
    }
    
//
}